//
//  main.m
//  05_排序、数组字典的处理
//
//  Created by dllo on 16/7/8.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
#import "Location.h"
#import "AreaCity.h"
#import "AreaDistrict.h"
#import "AreaProvince.h"

#import "LocProvince.h"
#import "LocCity.h"
#import "LocDistrict.h"

int main(int argc, const char * argv[]) {
#if 0

    /*
     一、
     1.创建1个包含5个字符串元素的数组；
     2.用for循环倒序输出所有元素；
     3.用for...in完成所有字符串元素的拼接(生成一个
     新的字符串)，并输出拼接好的字符串。
     */
    NSArray *strArr = @[@"haha", @"Xixi", @"Enen", @"Heihei", @"Hehe"];
    for (NSInteger i = strArr.count - 1; i >= 0; --i) {
        NSLog(@"%@", strArr[i]);
    }
    NSString *allStr = [NSString string];
    for (NSString *str in strArr) {
        allStr = [allStr stringByAppendingString:str];
    }
    NSLog(@"%@", allStr);
    
    
    /*
     二、
     
     1.创建1个包含5个person对象的数组(姓名用英文)；
     2.用for循环找到年龄最大的Person对象；
     3.用for...in遍历数组，把大于某个年龄的Person对象的姓名后面拼接上字符串“-brother”。
     */
    Person *p1 = [[Person alloc] initWithName:@"zhangsan" age:33 sex:@"nan"];
    Person *p2 = [[Person alloc] initWithName:@"lisi" age:66 sex:@"nv"];
    Person *p3 = [[Person alloc] initWithName:@"wangwu" age:44 sex:@"nv"];
    Person *p4 = [[Person alloc] initWithName:@"goushengzi" age:77 sex:@"nv"];
    Person *p5 = [[Person alloc] initWithName:@"erlengzi" age:55 sex:@"nan"];
    NSArray *perArr = @[p1, p2, p3, p4, p5];
    NSUInteger maxAgeIndex = [perArr[0] age];
    for (NSUInteger i = 0; i < perArr.count; i++) {
        maxAgeIndex = [perArr[i] age] > maxAgeIndex ? i : maxAgeIndex;
    }
    NSLog(@"年龄最大的是%@", perArr[maxAgeIndex]);

    NSInteger age = 88;
    NSLog(@"输入年龄:");
//    scanf("%ld", &age);
    for (Person *per in perArr) {
        if (per.age > age) {
            per.name = [per.name stringByAppendingString:@"-brother"];
        }
    }
    for (Person *per in perArr) {
        NSLog(@"%@", per);
    }
    
    /*
     三、
     
     1.使用第二题目中生成的数组；
     2.使用sortUsingSelector: 分别按年龄，姓名，性别进行排序，输出排序结果；
     3.使用NSSortDescriptor，按姓名进行排序。
     */
    NSMutableArray *mPerArr = [NSMutableArray arrayWithArray:perArr];
    [mPerArr sortUsingSelector:@selector(compareByAge:)];
    NSLog(@"按年龄排序:%@", mPerArr);
    [mPerArr sortUsingSelector:@selector(compareByName:)];
    NSLog(@"按姓名排序:%@", mPerArr);
    [mPerArr sortUsingSelector:@selector(compareBySex:)];
    NSLog(@"按性别排序:%@", mPerArr);
    
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    [mPerArr sortUsingDescriptors:@[sort]];
    NSLog(@"排序后:%@", mPerArr);
    
#endif
#if 0
    /*
     四、
     
     处理省市区文件中的area.txt
     将省市区进行分割处理, 省里包含市, 市里包含区
     1. 先用数组字典表示
     2. 将数组字典的处理方式转换成数组模型(Class)
     */
    NSString *path = @"/Users/dllo/DLS160710/Objective-C/HomeWork/05_排序、数组字典的处理/05_排序、数组字典的处理/area.txt";
    NSString *areaSource = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *lineArr = [areaSource componentsSeparatedByString:@"\n"];
    NSMutableArray *provinceArr = [NSMutableArray array];
    for (NSString *line in lineArr) {
        if ([line hasPrefix:@" "] == NO) {
            NSMutableDictionary *provinceDic = [NSMutableDictionary dictionary];
            [provinceDic setObject:line forKey:@"provinceName"];
            NSMutableArray *cityArr = [NSMutableArray array];
            [provinceDic setObject:cityArr forKey:@"cityGroup"];
            [provinceArr addObject:provinceDic];
        } else if ([line hasPrefix:@"  "] && [line hasPrefix:@"    "] == NO) {
            NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
            [cityDic setObject:[line substringFromIndex:2] forKey:@"cityName"];
            NSMutableArray *districtArr = [NSMutableArray array];
            [cityDic setObject:districtArr forKey:@"districtGroup"];
            [provinceArr.lastObject[@"cityGroup"] addObject:cityDic];
        } else {
            [[[provinceArr lastObject][@"cityGroup"] lastObject][@"districtGroup"] addObject:[line substringFromIndex:4]];
        }
    }
    NSLog(@"%@", provinceArr);
    
    // 转换成数组模型(Class)
    NSMutableArray *areaProvinceArr = [NSMutableArray array];
    for (NSString *line in lineArr) {
        if ([line hasPrefix:@" "] == NO) {
            AreaProvince *areaProvince = [[AreaProvince alloc] init];
            areaProvince.provinceName = line;
            NSMutableArray *cityArr = [NSMutableArray array];
            areaProvince.cityGroup = cityArr;
            [areaProvinceArr addObject:areaProvince];
        } else if ([line hasPrefix:@"  "] && [line hasPrefix:@"    "] == NO) {
            AreaCity *areaCity = [[AreaCity alloc] init];
            [areaCity setValue:[line substringFromIndex:2] forKey:@"cityName"];
            NSMutableArray *districtArr = [NSMutableArray array];
            [areaCity setValue:districtArr forKey:@"districtGroup"];
            [[areaProvinceArr.lastObject cityGroup] addObject:areaCity];
        } else {
            [[[[[areaProvinceArr lastObject] cityGroup] lastObject] districtGroup] addObject:[line substringFromIndex:4]];
        }
    }
//    NSLog(@"areaProvinceArr:%@", areaProvinceArr);
#endif
#if 1

    /*
     五、拓展
     
     上述作业做完, 处理Location1.txt
     要求同第四题。自己寻找规律
     */
    NSString *pathLocation = @"/Users/dllo/DLS160710/Objective-C/HomeWork/05_排序、数组字典的处理/05_排序、数组字典的处理/Location1.txt";
    NSString *locationSource = [NSString stringWithContentsOfFile:pathLocation encoding:NSUTF8StringEncoding error:nil];
    NSArray *lineArray = [locationSource componentsSeparatedByString:@","];
    
    NSMutableArray *sourceArr = [NSMutableArray array];
    for (NSString *str in lineArray) {
        NSArray *oneLocation = [str componentsSeparatedByString:@"|"];
        Location *location = [[Location alloc] init];
        location.name = oneLocation.firstObject;
        location.number = oneLocation.lastObject;
        [sourceArr addObject:location];
    }
    
//    NSSortDescriptor *sortByNumber = [[NSSortDescriptor alloc] initWithKey:@"number" ascending:YES];
//    [locationArr sortUsingDescriptors:@[sortByNumber]];
//    NSLog(@"%@", locationArr);
    
    NSMutableArray *locArr = [NSMutableArray array];
    for (Location *loc in sourceArr) {
        // 后四位是0000
        if ([[loc.number substringFromIndex:loc.number.length - 4] isEqualToString:@"0000"]) {               NSMutableDictionary *locationDic = [NSMutableDictionary dictionary];
            [locationDic setObject:loc.name forKey:@"provinceName"];
            [locationDic setObject:loc.number forKey:@"provinceNumber"];
            [locationDic setObject:[loc.number substringToIndex:3] forKey:@"abbrProvince"];
            NSMutableArray *cityGroup = [NSMutableArray array];
            [locationDic setObject:cityGroup forKey:@"cityGroup"];
            [locArr addObject:locationDic];
        } else if ([[loc.number substringFromIndex:loc.number.length - 2] isEqualToString:@"00"] && [loc.number characterAtIndex:loc.number.length - 3] != '0') {
            // 后两位是00,倒数第三位不是0,这是市,找到对应的字典
            for (NSMutableDictionary *procinceDic in locArr) {
                if ([procinceDic[@"abbrProvince"] isEqualToString:[loc.number substringToIndex:3]]) {
                    NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
                    [cityDic setObject:loc.name forKey:@"cityName"];
                    [cityDic setObject:loc.number forKey:@"cityNumber"];
                    [cityDic setObject:[loc.number substringToIndex:4] forKey:@"abbrCity"];
                    NSMutableArray *districtGroup = [NSMutableArray array];
                    [cityDic setObject:districtGroup forKey:@"districtGroup"];
                    [procinceDic[@"cityGroup"] addObject:cityDic];
                    break;
                }
            }
        } else {
            // 剩下的都是区
            for (NSMutableDictionary *procinceDic in locArr) {
                // 找到对应的省
                if ([procinceDic[@"abbrProvince"] isEqualToString:[loc.number substringToIndex:3]]) {
                    // 找到对应的市
                    BOOL isMunicipality = YES;    // 直辖市
                    for (NSMutableDictionary *cityDic in procinceDic[@"cityGroup"]) {
                        if ([cityDic[@"abbrCity"] isEqualToString:[loc.number substringToIndex:4]]) {
                            NSMutableDictionary *districtDic = [NSMutableDictionary dictionary];
                            [districtDic setObject:loc.name forKey:@"districtName"];
                            [districtDic setObject:loc.number forKey:@"districtNumber"];
                            [cityDic[@"districtGroup"] addObject:districtDic];
                            isMunicipality = NO;
                            break;
                        }
                    }
                    // 直辖市没有1101
                    if (isMunicipality) {
                        if (procinceDic[@"municipalityGroup"]) {
                            NSMutableDictionary *districtDic = [NSMutableDictionary dictionary];
                            [districtDic setObject:loc.name forKey:@"districtName"];
                            [districtDic setObject:loc.number forKey:@"districtNumber"];
                            [procinceDic[@"municipalityGroup"] addObject:districtDic];
                        } else {
                            NSMutableArray *municipality = [NSMutableArray array];
                            [procinceDic setObject:municipality forKey:@"municipalityGroup"];
                        }
                    }
                }
            }
        }
    }
    
//    NSLog(@"%@", locArr);
    
    // 转换成数组模型(Class)
    NSMutableArray *locArray = [NSMutableArray array];
    for (Location *loc in sourceArr) {
        // 后四位是0000, 后四位用hasSuffix方法
        if ([[loc.number substringFromIndex:loc.number.length - 4] isEqualToString:@"0000"]) {               LocProvince *locProvince = [[LocProvince alloc] init];
            [locProvince setValue:loc.name forKey:@"provinceName"];
            [locProvince setValue:loc.number forKey:@"provinceNumber"];
            [locProvince setValue:[loc.number substringToIndex:3] forKey:@"abbrProvince"];
            NSMutableArray *cityGroup = [NSMutableArray array];
            [locProvince setValue:cityGroup forKey:@"cityGroup"];
            [locArray addObject:locProvince];
        } else if ([[loc.number substringFromIndex:loc.number.length - 2] isEqualToString:@"00"] && [loc.number characterAtIndex:loc.number.length - 3] != '0') {
            // 后两位是00,倒数第三位不是0,这是市,找到对应的字典
            for (LocProvince *locProvince in locArray) {
                if ([locProvince.abbrProvince isEqualToString:[loc.number substringToIndex:3]]) {
                    LocCity *locCity = [[LocCity alloc] init];
                    [locCity setValue:loc.name forKey:@"cityName"];
                    [locCity setValue:loc.number forKey:@"cityNumber"];
                    [locCity setValue:[loc.number substringToIndex:4] forKey:@"abbrCity"];
                    NSMutableArray *districtGroup = [NSMutableArray array];
                    [locCity setValue:districtGroup forKey:@"districtGroup"];
                    [locProvince.cityGroup addObject:locCity];
                    break;
                }
            }
        } else {
            // 剩下的都是区
            for (LocProvince *locProvince in locArray) {
                // 找到对应的省
                if ([locProvince.abbrProvince isEqualToString:[loc.number substringToIndex:3]]) {
                    // 找到对应的市
                    BOOL isMunicipality = YES;    // 直辖市
                    for (LocCity *locCity in locProvince.cityGroup) {
                        if ([locCity.abbrCity isEqualToString:[loc.number substringToIndex:4]]) {
                            LocDistrict *locDistrict = [[LocDistrict alloc] init];
                            [locDistrict setValue:loc.name forKey:@"districtName"];
                            [locDistrict setValue:loc.number forKey:@"districtNumber"];
                            [locCity.districtGroup addObject:locDistrict];
                            isMunicipality = NO;
                            break;
                        }
                    }
                    // 直辖市没有1101
                    if (isMunicipality) {
                        if (locProvince.municipalityGroup) {
                            LocDistrict *locDistrict = [[LocDistrict alloc] init];
                            [locDistrict setValue:loc.name forKey:@"districtName"];
                            [locDistrict setValue:loc.number forKey:@"districtNumber"];
                            [locProvince.municipalityGroup addObject:locDistrict];
                        } else {
                            NSMutableArray *municipality = [NSMutableArray array];
                            [locProvince setValue:municipality forKey:@"municipalityGroup"];
                        }
                    }
                }
            }
        }
    }
    
    NSLog(@"%@", locArray);
    
#endif
    return 0;
}
